﻿using ComponentFactory.Krypton.Toolkit;
using SRB.Tg.Model;
using SRB.Tg.Operate;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Teigha.DatabaseServices;

namespace SRB.Tg.Cad
{
    public partial class PileCapManagerForm : KryptonForm
    {
        DataRow _row;
        double _scale = 50;
        public DataRow myrow;
        public bool isConfirm = false;
        private static PileCapManagerForm _instance;
        public static PileCapManagerForm _instanceAdd;
        public static PileCapManagerForm getInstance(DataRow row, double scale, bool isModify = false)
        {
            if (_instance == null)
            {
                _instance = new PileCapManagerForm(row, scale);
            }
            else
            {
                if (!isModify)
                {
                    LoadDate(_instance, row, scale);
                }
                _instance.Refresh();
            }
            return _instance;
        }

        public static PileCapManagerForm getInstance(bool isNew = false)
        {
            if (_instanceAdd == null)
            {
                _instanceAdd = new PileCapManagerForm();
            }
            else
            {
                if (isNew)
                    _instanceAdd = new PileCapManagerForm();
            }
            return _instanceAdd;
        }

        public PileCapManagerForm()
        {
            InitializeComponent();
            InitDic();
            this.Refresh();
        }
        public void InitDic()
        {
            string sql = "select dict_label from system_dict_data where dict_type='reinforcement_grade'";

            System.Data.DataTable dt = MySqlHelper.ExecuteDataTable(sql, CommandType.Text);
            if (dt.Rows.Count > 0)
            {
                reinforcement_grade.DataSource = dt;
                reinforcement_grade.DisplayMember = "dict_label";
                reinforcement_grade.ValueMember = "dict_label";
                reinforcement_grade.Text = "B500B";
            }
        }
        public PileCapManagerForm(DataRow row, double scale)
        {
            InitializeComponent();
            _row = row;
            _scale = scale;
            this.configure_name.Text = row["configure_name"].ToString();
            this.around_l_reinforcement_diameter.Text = row["around_l_reinforcement_diameter"].ToString();
            this.around_reinforcement_spacing.Text = row["around_reinforcement_spacing"].ToString();
            this.around_w_reinforcement_diameter.Text = row["around_w_reinforcement_diameter"].ToString();
            this.around_h_reinforcement_diameter.Text = row["around_h_reinforcement_diameter"].ToString();
            this.around_h_reinforcement_spacing.Text = row["around_h_reinforcement_spacing"].ToString();
            this.bottom_l_reinforcement_diameter.Text = row["bottom_l_reinforcement_diameter"].ToString();
            this.bottom_l_reinforcement_spacing.Text = row["bottom_l_reinforcement_spacing"].ToString();
            this.bottom_reinforcement_layers_number.Text = row["bottom_reinforcement_layers_number"].ToString();
            this.bottom_reinforcement_layers_spacing.Text = row["bottom_reinforcement_layers_spacing"].ToString();
            this.bottom_w_reinforcement_diameter.Text = row["bottom_w_reinforcement_diameter"].ToString();
            this.bottom_w_reinforcement_spacing.Text = row["bottom_w_reinforcement_spacing"].ToString();

            this.concrete_protect_layer.Text = row["concrete_protect_layer"].ToString();

            this.erection_reinforcement_diameter.Text = row["erection_reinforcement_diameter"].ToString();
            this.erection_reinforcement_l_spacing.Text = row["erection_reinforcement_l_spacing"].ToString();
            this.erection_reinforcement_w_spacing.Text = row["erection_reinforcement_w_spacing"].ToString();
            this.length.Text = row["length"].ToString();
            this.width.Text = row["width"].ToString();
            this.height.Text = row["height"].ToString();

            this.reinforcement_grade.Text = row["reinforcement_grade"].ToString();

            this.top_l_reinforcement_diameter.Text = row["top_l_reinforcement_diameter"].ToString();
            this.top_l_reinforcement_spacing.Text = row["top_l_reinforcement_spacing"].ToString();
            this.top_reinforcement_layers_number.Text = row["top_reinforcement_layers_number"].ToString();
            this.top_reinforcement_layers_spacing.Text = row["top_reinforcement_layers_spacing"].ToString();
            this.top_w_reinforcement_diameter.Text = row["top_w_reinforcement_diameter"].ToString();
            this.top_w_reinforcement_spacing.Text = row["top_w_reinforcement_spacing"].ToString();

            this.cushion_cornice_length.Text = row["cushion_cornice_length"].ToString();
            this.cushion_thickness.Text = row["cushion_thickness"].ToString();
            this.pile_diameter.Text = row["pile_diameter"].ToString();
            this.pile_layout.Text = row["pile_layout"].ToString();
            this.pier_type.Text = row["pier_type"].ToString();
            this.pier_size.Text = row["pier_size"].ToString();
            this.pier_layout.Text = row["pier_layout"].ToString();
        }
        public static void LoadDate(PileCapManagerForm _instance, DataRow row, double scale)
        {
            _instance._row = row;
            _instance._scale = scale;

            _instance.configure_name.Text = row["configure_name"].ToString();
            _instance.around_l_reinforcement_diameter.Text = row["around_l_reinforcement_diameter"].ToString();
            _instance.around_reinforcement_spacing.Text = row["around_reinforcement_spacing"].ToString();
            _instance.around_w_reinforcement_diameter.Text = row["around_w_reinforcement_diameter"].ToString();
            _instance.around_h_reinforcement_diameter.Text = row["around_h_reinforcement_diameter"].ToString();
            _instance.around_h_reinforcement_spacing.Text = row["around_h_reinforcement_spacing"].ToString();
            _instance.bottom_l_reinforcement_diameter.Text = row["bottom_l_reinforcement_diameter"].ToString();
            _instance.bottom_l_reinforcement_spacing.Text = row["bottom_l_reinforcement_spacing"].ToString();
            _instance.bottom_reinforcement_layers_number.Text = row["bottom_reinforcement_layers_number"].ToString();
            _instance.bottom_reinforcement_layers_spacing.Text = row["bottom_reinforcement_layers_spacing"].ToString();
            _instance.bottom_w_reinforcement_diameter.Text = row["bottom_w_reinforcement_diameter"].ToString();
            _instance.bottom_w_reinforcement_spacing.Text = row["bottom_w_reinforcement_spacing"].ToString();

            _instance.concrete_protect_layer.Text = row["concrete_protect_layer"].ToString();

            _instance.erection_reinforcement_diameter.Text = row["erection_reinforcement_diameter"].ToString();
            _instance.erection_reinforcement_l_spacing.Text = row["erection_reinforcement_l_spacing"].ToString();
            _instance.erection_reinforcement_w_spacing.Text = row["erection_reinforcement_w_spacing"].ToString();

            _instance.length.Text = row["length"].ToString();
            _instance.width.Text = row["width"].ToString();
            _instance.height.Text = row["height"].ToString();

            _instance.reinforcement_grade.Text = row["reinforcement_grade"].ToString();

            _instance.top_l_reinforcement_diameter.Text = row["top_l_reinforcement_diameter"].ToString();
            _instance.top_l_reinforcement_spacing.Text = row["top_l_reinforcement_spacing"].ToString();          
            _instance.top_reinforcement_layers_number.Text = row["top_reinforcement_layers_number"].ToString();
            _instance.top_reinforcement_layers_spacing.Text = row["top_reinforcement_layers_spacing"].ToString();
            _instance.top_w_reinforcement_diameter.Text = row["top_w_reinforcement_diameter"].ToString();
            _instance.top_w_reinforcement_spacing.Text = row["top_w_reinforcement_spacing"].ToString();

            _instance.cushion_cornice_length.Text = row["cushion_cornice_length"].ToString();
            _instance.cushion_thickness.Text = row["cushion_thickness"].ToString();
            _instance.pile_diameter.Text = row["pile_diameter"].ToString();
            _instance.pile_layout.Text = row["pile_layout"].ToString();
            _instance.pier_type.Text = row["pier_type"].ToString();
            _instance.pier_size.Text = row["pier_size"].ToString();
            _instance.pier_layout.Text = row["pier_layout"].ToString();
        }

        public void SaveAdd()
        {
            if (string.IsNullOrWhiteSpace(this.configure_name.Text))
            {
                MessageBox.Show("请填写承台名称！", "提示");
                return;
            }
            else
            {
                string sqlS = "select * from t_pile_cap_reinforcement where configure_name='" + this.configure_name.Text + "'";

                System.Data.DataTable dt = MySqlHelper.ExecuteDataTable(sqlS, CommandType.Text);
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("承台名称" + this.configure_name.Text + "已存在，请修改承台名称！", "提示");
                    return;
                }
                if (string.IsNullOrWhiteSpace(this.length.Text))
                {
                    MessageBox.Show("请填写横向长度！", "提示");
                    return;
                }
                if (string.IsNullOrWhiteSpace(this.height.Text))
                {
                    MessageBox.Show("请填写竖向高度！", "提示");
                    return;
                }
                if (string.IsNullOrWhiteSpace(this.width.Text))
                {
                    MessageBox.Show("请填写纵向宽度！", "提示");
                    return;
                }
                //if (string.IsNullOrWhiteSpace(this.width.Text))
                //{
                //    MessageBox.Show("请填写纵向宽度！", "提示");
                //    return;
                //}
                string sql = string.Format("insert into t_pile_cap_reinforcement(around_h_reinforcement_diameter,around_h_reinforcement_spacing,around_l_reinforcement_diameter,"
                    + "around_reinforcement_spacing,around_w_reinforcement_diameter,bottom_l_reinforcement_diameter,bottom_l_reinforcement_spacing,bottom_reinforcement_layers_number,"
                    + "bottom_reinforcement_layers_spacing,bottom_w_reinforcement_diameter,bottom_w_reinforcement_spacing,concrete_protect_layer,"
                    + "erection_reinforcement_diameter,erection_reinforcement_l_spacing,erection_reinforcement_w_spacing,height,length,reinforcement_grade,"
                    + "top_l_reinforcement_diameter,top_l_reinforcement_spacing,top_reinforcement_layers_number,top_w_reinforcement_diameter,top_w_reinforcement_spacing,"
                    + "width,top_reinforcement_layers_spacing,cushion_thickness,cushion_cornice_length,pile_diameter,pile_layout,pier_type,pier_size,pier_layout,create_time) "
                    + "values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}','{25}','{26}','{27}','{28}','{29}','{30}','{31}','{32}','{33}','{34}')",
                     
                    this.around_h_reinforcement_diameter.Text, this.around_h_reinforcement_spacing.Text, this.around_l_reinforcement_diameter.Text,
                       this.around_reinforcement_spacing.Text, this.around_w_reinforcement_diameter.Text, this.bottom_l_reinforcement_diameter.Text,
                       this.bottom_l_reinforcement_spacing.Text, this.bottom_reinforcement_layers_number.Text, bottom_reinforcement_layers_spacing.Text, 
                       this.bottom_w_reinforcement_diameter.Text, this.bottom_w_reinforcement_spacing.Text, this.concrete_protect_layer.Text, 
                       this.erection_reinforcement_diameter.Text, this.erection_reinforcement_l_spacing.Text, erection_reinforcement_w_spacing.Text,
                       this.height.Text, this.length.Text, this.reinforcement_grade.Text, 
                       this.top_l_reinforcement_diameter.Text, this.top_l_reinforcement_spacing.Text,this.top_reinforcement_layers_number.Text, 
                       this.top_w_reinforcement_diameter.Text, this.top_w_reinforcement_spacing.Text,  width.Text,
                       top_reinforcement_layers_spacing.Text,  cushion_thickness.Text, cushion_cornice_length.Text,
                       pile_diameter.Text, pile_layout.Text, pier_type.Text, pier_size.Text, pier_layout.Text, 
                       DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), configure_name.Text, concrete_no.Text);

                if (MySqlHelper.ExecuteNonQuery(sql, CommandType.Text) > 0)
                {
                    string sqlM = "select * from t_pile_cap_reinforcement where configure_name='" + this.configure_name.Text + "'";
                    System.Data.DataTable dtM = MySqlHelper.ExecuteDataTable(sqlM, CommandType.Text);
                    if (dtM.Rows.Count > 0)
                    {
                        myrow = dtM.Rows[0];
                    }
                    isConfirm = true;
                }
            }
        }
        public void SavModify()
        {
            if (_row != null)
            {
                if (!_row.IsNull("id"))
                {
                    string updatesql = string.Format("update t_pile_cap_reinforcement set around_h_reinforcement_diameter='{1}',"
                        + "around_h_reinforcement_spacing='{2}',around_l_reinforcement_diameter='{3}',around_reinforcement_spacing='{4}',around_w_reinforcement_diameter='{5}',"
                        + "bottom_l_reinforcement_diameter='{6}',bottom_l_reinforcement_spacing='{7}',bottom_reinforcement_layers_number='{8}',bottom_w_reinforcement_diameter='{9}',"
                        + "bottom_w_reinforcement_spacing='{10}',concrete_protect_layer='{11}',"
                        + "erection_reinforcement_diameter='{12}',erection_reinforcement_l_spacing='{13}',height='{14}',length='{15}',"
                        + "reinforcement_grade='{16}',top_l_reinforcement_diameter='{17}',top_l_reinforcement_spacing='{18}',top_reinforcement_layers_number='{19}',"
                        + "top_w_reinforcement_diameter='{20}',top_w_reinforcement_spacing='{21}',erection_reinforcement_w_spacing='{22}',width='{23}',"
                        + "top_reinforcement_layers_spacing='{24}',bottom_reinforcement_layers_spacing='{25}',cushion_thickness='{26}',cushion_cornice_length='{27}',"
                        + "pile_diameter='{28}',pile_layout='{29}',pier_type='{30}',pier_size='{31}',pier_layout='{32}',update_time='{33}',configure_name='{34}',concrete_no='{35}'"
                        + " where id='{0}'", _row["id"].ToString(),

                       this.around_h_reinforcement_diameter.Text, this.around_h_reinforcement_spacing.Text, this.around_l_reinforcement_diameter.Text,
                       this.around_reinforcement_spacing.Text, this.around_w_reinforcement_diameter.Text, this.bottom_l_reinforcement_diameter.Text,
                       this.bottom_l_reinforcement_spacing.Text, this.bottom_reinforcement_layers_number.Text, this.bottom_w_reinforcement_diameter.Text,
                       this.bottom_w_reinforcement_spacing.Text, this.concrete_protect_layer.Text, this.erection_reinforcement_diameter.Text, this.erection_reinforcement_l_spacing.Text,
                       this.height.Text, this.length.Text,this.reinforcement_grade.Text,this.top_l_reinforcement_diameter.Text, this.top_l_reinforcement_spacing.Text, this.top_reinforcement_layers_number.Text,
                       this.top_w_reinforcement_diameter.Text, this.top_w_reinforcement_spacing.Text, erection_reinforcement_w_spacing.Text,width.Text,
                       top_reinforcement_layers_spacing.Text,bottom_reinforcement_layers_spacing.Text,cushion_thickness.Text,cushion_cornice_length.Text,
                       pile_diameter.Text, pile_layout.Text, pier_type.Text, pier_size.Text, pier_layout.Text, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"),configure_name.Text, concrete_no.Text);

                    if (MySqlHelper.ExecuteNonQuery(updatesql, CommandType.Text) > 0)
                    {
                        string sqlS = "select * from t_pile_cap_reinforcement where id='" + _row["id"].ToString() + "'";
                        System.Data.DataTable dt = MySqlHelper.ExecuteDataTable(sqlS, CommandType.Text);
                        if (dt.Rows.Count > 0)
                        {
                            myrow = dt.Rows[0];
                        }
                        isConfirm = true;
                    }


                }
            }
        }
    }
}
